package com.padakeji.android.ui.widget;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.padakeji.android.ui.R;

/**
 * Created by atanl on 2016-01-06.
 */
public class SimpleSlide extends AdapterViewFlipperX {

    private DataSetObserver mObserver;
    private LinearLayout mIndicatorWrapper;
    private int mIndicatorMargin;
    private OnItemSelectedListener mOnItemSelectedListener;

    public SimpleSlide(Context context) {
        this(context, null);
    }

    public SimpleSlide(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mObserver = new DataSetObserver() {
            @Override
            public void onChanged() {
                mIndicatorWrapper.removeAllViews();
                Adapter adapter = getAdapter();
                int count = adapter.getCount();
                if (count > 1) {
                    for (int i = 0; i < count; ++i) {
                        View v = getIndicatorView();
                        v.setSelected(i == getDisplayedChild());
                        mIndicatorWrapper.addView(v);
                    }
                }
                invalidate();
            }

            @Override
            public void onInvalidated() {
            }
        };

        mIndicatorWrapper = new LinearLayout(getContext());
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mIndicatorWrapper.setLayoutParams(params);
        mIndicatorWrapper.setOrientation(LinearLayout.HORIZONTAL);
        mIndicatorWrapper.setGravity(Gravity.CENTER_VERTICAL);

        mIndicatorMargin = getResources().getDimensionPixelSize(R.dimen.padaui_SimpleSlide_margin);

        super.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                int count = mIndicatorWrapper.getChildCount();
                if (count > 0) {
                    for (int i = 0; i < count; ++i) {
                        View v = mIndicatorWrapper.getChildAt(i);
                        v.setSelected(i == position);
                    }
                }
                if (mOnItemSelectedListener != null) {
                    mOnItemSelectedListener.onItemSelected(parent, view, position, id);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                if (mOnItemSelectedListener != null) {
                    mOnItemSelectedListener.onNothingSelected(parent);
                }
            }
        });

    }

    @Override
    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.mOnItemSelectedListener = listener;
    }

    @Override
    public void setAdapter(Adapter adapter) {
        Adapter originalAdapter = getAdapter();
        if (originalAdapter != null) {
            originalAdapter.unregisterDataSetObserver(mObserver);
        }
        super.setAdapter(adapter);
        if (adapter != null) {
            adapter.registerDataSetObserver(mObserver);
            mObserver.onChanged();
        }
    }

    private View getIndicatorView() {
        ImageView v = new ImageView(getContext());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(mIndicatorMargin, mIndicatorMargin, mIndicatorMargin, mIndicatorMargin);
        v.setLayoutParams(params);
        v.setImageDrawable(getResources().getDrawable(R.drawable.padaui_adapterviewflipperx_indicator));
        return v;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int measureWidth = getMeasuredWidth();
        int measureHeight = getMeasuredHeight();

        int wms = MeasureSpec.makeMeasureSpec(measureWidth, MeasureSpec.AT_MOST);
        int wmh = MeasureSpec.makeMeasureSpec(measureHeight, MeasureSpec.AT_MOST);

        measureChild(mIndicatorWrapper, wms, wmh);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        int indicatorWrapperWidth = mIndicatorWrapper.getMeasuredWidth();
        int indicatorWrapperHeight = mIndicatorWrapper.getMeasuredHeight();

        mIndicatorWrapper.layout(width - indicatorWrapperWidth, height - indicatorWrapperHeight, width, height);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        drawChild(canvas, mIndicatorWrapper, getDrawingTime()); // 把指示器画上去，指示器一直都在，只是当元素数量<=1时，大小为0而已。
    }
}
